package com.ruoyi.shop.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.shop.api.domain.CheDrawal;
import com.ruoyi.shop.service.ICheDrawalService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 商家提现记录Controller
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/drawal")
public class CheDrawalController extends BaseController
{
    @Autowired
    private ICheDrawalService cheDrawalService;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 查询商家提现记录列表
     */
    @RequiresPermissions("shop:drawal:list")
    @GetMapping("/list")
    public TableDataInfo list(CheDrawal cheDrawal)
    {
        startPage();
        List<CheDrawal> list = cheDrawalService.selectCheDrawalList(cheDrawal);
        return getDataTable(list);
    }
    /**
     * 查询商家提现记录列表
     */
    @RequiresPermissions("shop:drawal:list")
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(CheDrawal cheDrawal)
    {
        List<CheDrawal> list = cheDrawalService.selectCheDrawalList(cheDrawal);
        return getDataTable(list);
    }

    /**
     * 导出商家提现记录列表
     */
    @RequiresPermissions("shop:drawal:export")
    @Log(title = "商家提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheDrawal cheDrawal)
    {
        List<CheDrawal> list = cheDrawalService.selectCheDrawalList(cheDrawal);
        ExcelUtil<CheDrawal> util = new ExcelUtil<CheDrawal>(CheDrawal.class);
        util.exportExcel(response, list, "商家提现记录数据");
    }

    /**
     * 获取商家提现记录详细信息
     */
    @RequiresPermissions("shop:drawal:query")
    @GetMapping(value = "/{drawalId}")
    public AjaxResult getInfo(@PathVariable("drawalId") Long drawalId)
    {
        return success(cheDrawalService.selectCheDrawalByDrawalId(drawalId));
    }

    /**
     * 新增商家提现记录
     */
    @RequiresPermissions("shop:drawal:add")
    @Log(title = "商家提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheDrawal cheDrawal, HttpServletRequest request)
    {
        int i = cheDrawalService.insertCheDrawal(cheDrawal);
        System.out.println(cheDrawal.getDrawalId());
        Date date = new Date();
        cheDrawal.setVerificationTime(date);
        //RabbitMQ获取头部权限
        String authorization = request.getHeader("Authorization");
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("Authorization",authorization);
        Message message = new Message(JSON.toJSONString(cheDrawal).getBytes(), messageProperties);
        if(i != 0){
            rabbitTemplate.convertAndSend("order_topic_exchange", "drawal.insert", message);
        }
        return toAjax(i);
    }

    /**
     * 修改商家提现记录
     */
    @RequiresPermissions("shop:drawal:edit")
    @Log(title = "商家提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheDrawal cheDrawal)
    {
        return toAjax(cheDrawalService.updateCheDrawal(cheDrawal));
    }

    /**
     * 删除商家提现记录
     */
    @RequiresPermissions("shop:drawal:remove")
    @Log(title = "商家提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{drawalIds}")
    public AjaxResult remove(@PathVariable Long[] drawalIds)
    {
        return toAjax(cheDrawalService.deleteCheDrawalByDrawalIds(drawalIds));
    }
}
